﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using eschoolBO;

namespace ESCHOOL.admin
{
    public partial class WebForm13 : System.Web.UI.Page
    {
        Classes cs = new Classes(); teacher teach = new teacher();
        protected void Page_Load(object sender, EventArgs e)
        {
            btnupdate.Visible = false;
            if (!IsPostBack)
            {
                DataTable dt1 = teach.GetRecords(-99, "", "", "", "", "", "", -99, -99, -99, "", "","","","", false, " and ");
                ddteacher.DataSource = dt1;
                ddteacher.DataTextField = "teacher_name";
                ddteacher.DataValueField = "teacher_id";
                ddteacher.DataBind();

                DataTable dt = cs.GetRecords(-99, "", -99, false, " and ");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            btnupdate.Visible = true;
            btninsert.Visible = false;
            DataTable dt = cs.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, false, " and ");
            tbclassid.Text = dt.Rows[0]["class_id"].ToString();
            tbclass.Text = dt.Rows[0]["class_name"].ToString();
            ddteacher.SelectedValue = dt.Rows[0]["teacher_id"].ToString();
        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = cs.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, false, " and ");
                cs.class_id = Convert.ToInt64(dt.Rows[0]["class_id"].ToString());
                cs.class_name = dt.Rows[0]["class_name"].ToString();
                cs.teacher_id = Convert.ToInt64(dt.Rows[0]["teacher_id"].ToString());
                cs.isdeleted = true;
                cs.SaveRecords();
                Response.Redirect("~/admin/Class.aspx");
            }
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            
            DataTable dt=cs.GetRecords(-99,"",-99,false," and ");
            bool f =false;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (ddteacher.SelectedValue.ToString() == dt.Rows[i]["teacher_id"].ToString())
                {
                    Response.Write("This teacher already a class teacher of" + dt.Rows[i]["class_id"].ToString());
                    f = true;
                }
            }
            if (f == true)
            
                Response.Redirect("~/admin/Class.aspx");
            else
            {
                cs.class_id = -99;
                cs.class_name = tbclass.Text;
                cs.teacher_id = Convert.ToInt64(ddteacher.SelectedValue.ToString());
                cs.isdeleted = false;
                cs.SaveRecords();
                Response.Redirect("~/admin/Class.aspx");
            }
        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            DataTable dt=cs.GetRecords(-99,"",-99,false," and ");
            bool f =false;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (ddteacher.SelectedValue.ToString() == dt.Rows[i]["teacher_id"].ToString())
                {
                   // Response.Write("This teacher already a class teacher of" + dt.Rows[i]["class_id"].ToString());
                    f = true;
                }
            }
            if (f == false)
            {
                cs.class_id = Convert.ToInt64(tbclassid.Text);
                cs.class_name = tbclass.Text;
                cs.teacher_id = Convert.ToInt64(ddteacher.SelectedValue.ToString());
                cs.isdeleted = false;
                cs.SaveRecords();
                Response.Redirect("~/admin/Class.aspx");
            }
            else
                Response.Redirect("~/admin/Class.aspx");
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }
    }
}
